#include "TimeStamp.h"
#include <ctime>

TimeStamp::TimeStamp(): microSecondsSinceEpoch_(0) {}

TimeStamp::TimeStamp(int64_t microSecondsSinceEpoch)
    : microSecondsSinceEpoch_(microSecondsSinceEpoch) {}

TimeStamp TimeStamp::now()
{
    return TimeStamp(time(NULL));
}

std::string TimeStamp::toString() const
{
    char buf[128] = {0};
    tm* tm_time = localtime(&microSecondsSinceEpoch_);
    snprintf(buf, 128, "%4d/%02d/%02d %02d:%02d:%02d", 
        tm_time->tm_year + 1900,
        tm_time->tm_mon + 1,
        tm_time->tm_mday,
        tm_time->tm_hour,
        tm_time->tm_min,
        tm_time->tm_sec);
    return buf;
}

std::string TimeStamp::toStringForFileName() const
{
    char buf[64] = {0};
    tm* tm_time = localtime(&microSecondsSinceEpoch_);
    snprintf(buf, 64, "%4d_%02d_%02d", 
        tm_time->tm_year + 1900,
        tm_time->tm_mon + 1,
        tm_time->tm_mday);
    return buf;
}

// #include <iostream>

// int main()
// {
//     std::cout << TimeStamp::now().toStringForLog() << std::endl;        // 2023/10/21 23:54:34
//     std::cout << TimeStamp::now().toStringForFileName() << std::endl;   // 2023_10_21
//     return 0;
// }
